package com.demo.project_template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.demo.project_template.entity.PostLike;
import com.demo.project_template.entity.UserLike;
import com.demo.project_template.mapper.UserLikeMapper;
import com.demo.project_template.service.UserLikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户喜欢用户记录 服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-08-20 11:48:58
 */
@Service
public class UserLikeServiceImpl extends ServiceImpl<UserLikeMapper, UserLike> implements UserLikeService {

    @Override
    public void add(UserLike userLike) {
        UserLike one = getOne(new LambdaQueryWrapper<UserLike>().eq(UserLike::getUserId, userLike.getUserId()).eq(UserLike::getLikeUserId, userLike.getLikeUserId()));
        //如果已有点赞记录则取消点赞
        if (one != null){
            removeById(one);
            return;
        }
        //如果未点赞则进行点赞
        save(userLike);
    }
}
